home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Visual Database / Visual BASIC 5.0 (Ent. Edition) / Vb5ent Extractor.EXE / VB / SAMPLES / COMPTOOL / ACTVCOMP / COFFEE / CO2CMON.CLS < prev    next >
Encoding:
Visual Basic class definition  |  1996-11-27  |  4.8 KB  |  122 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4. END
  5. Attribute VB_Name = "CoffeeMonitor"
  6. Attribute VB_GlobalNameSpace = False
  7. Attribute VB_Creatable = False
  8. Attribute VB_PredeclaredId = False
  9. Attribute VB_Exposed = True
  10. Option Explicit
  11. ' > For an overview of this sample application, search
  12. '   online Help for Coffee.
  13. ' > AboutCof.Txt, in the Related Documents folder of
  14. '   CoffWat2.vbp, also contains information about the sample.
  15.  
  16. ' CoffeeMonitor class monitors an imaginary serial interface
  17. ' -------------------   to a high-tech coffee pot, using
  18. '   a call-back timer (XTimer object) to determine how
  19. '   often to check the coffee status.  When the coffee's
  20. '   ready, the CoffeeMonitor object raises an event to
  21. '   notify clients.  (Since the high-tech coffee pot has
  22. '   not yet been invented, this sample application just
  23. '   raises the CoffeeReady event every ten seconds.)
  24. '
  25. ' (See also the CoffeeMonitor2 class, which demonstrates
  26. '   notification by a call-back method instead of an event.)
  27. '
  28. ' Note that the CoffeeMonitor class's Instancing property
  29. '   is set to PublicNotCreatable.  This means that clients
  30. '   cannot create CoffeeMonitors; they can only get a
  31. '   reference to the shared CoffeeMonitor by creating a
  32. '   Connector object and accessing its CoffeeMonitor
  33. '   property.
  34. '
  35. ' By using a code-only timer, this version of CoffeeMonitor
  36. '   fixes the bug described in the topic "Using the Shared
  37. '   CoffeeMonitor," in "Creating an ActiveX Exe Component,"
  38. '   in Books Online, whereby multiple CoffeeMonitor objects
  39. '   could sometimes be created.
  40. '
  41. ' That bug was caused by the 'use count' code the Connector
  42. '   object used to release the global reference to the shared
  43. '   CoffeeMonitor.  The use count was required because the
  44. '   step-by-step procedures in Books Online used a hidden
  45. '   form to hold a Timer control; hidden forms keep
  46. '   components from unloading (as described in "Starting and
  47. '   Ending a Component" in Books Online), which keeps global
  48. '   memory from being freed, which keeps gCoffeeMonitor from
  49. '   being set to Nothing.
  50. '
  51. ' Unlike the hidden form with its Timer control, the XTimer
  52. '   object won't keep the Coffee2 component from unloading
  53. '   when the last client releases its last reference.  (Note,
  54. '   however, that the global variable gCoffeeMonitor will
  55. '   keep the CoffeeMonitor object alive until all objects
  56. '   provided by Coffee2 are released by their respective
  57. '   clients.)
  58.  
  59. ' =======================================================
  60. '  WARNING!  Code-only timers are inherently dangerous
  61. '       in the Visual Basic development environment,
  62. '       becaue the system blindly calls back into your
  63. '       code until the timer is turned off with an API
  64. '       call.  It's safer to use Timer controls during
  65. '       most of the development process, and only switch
  66. '       to call-back timers at the very end.
  67. ' =======================================================
  68.  
  69.  
  70. ' mXTimer holds a reference to a code-only timer that
  71. ' -------   tells CoffeeMonitor when to check the pot.
  72. '   Because the variable is declared WithEvents, the
  73. '   CoffeeMonitor object receives the XTimer object's Tick
  74. '   events (see Sub mwXTimer_Tick, below).  Code for the
  75. '   XTimer object can be found in XTimers.vbp.
  76. Private WithEvents mwXTimer As XTimer
  77. Attribute mwXTimer.VB_VarHelpID = -1
  78.  
  79. ' CoffeeReady is the event CoffeeMonitor raises for its
  80. ' -----------   clients when the coffee is done.  The
  81. '   Public keyword is omitted from the event declaration
  82. '   because events are always public.
  83. Event CoffeeReady()
  84.     
  85. Private Sub Class_Initialize()
  86.     ' The first thing a CoffeeMonitor object does is
  87.     '   create the XTimer object.  When this assignment is
  88.     '   made, Visual Basic connects the XTimer's Tick event
  89.     '   to the mwXTimer_Tick event procedure (see below).
  90.     '
  91.     Set mwXTimer = New XTimer
  92.     '
  93.     ' The timer is set to tick every ten seconds (10,000
  94.     '   milliseconds).
  95.     mwXTimer.Interval = 10000
  96.     mwXTimer.Enabled = True
  97. End Sub
  98.  
  99. Private Sub Class_Terminate()
  100.     ' It's important to disable the XTimer before releasing
  101.     '   it.  As described in XTimers.vbp, abandoning a
  102.     '   running XTimer essentially leaks a system timer
  103.     '   until XTimers.DLL finally shuts down.
  104.     mwXTimer.Enabled = False
  105.     Set mwXTimer = Nothing
  106.     '
  107.     Debug.Print "CoffeeMonitor (events) terminated at " & Now
  108. End Sub
  109.  
  110. ' mwXTimer_Tick is the event procedure CoffeeMonitor uses
  111. ' -------------   to receive the XTimer object's Tick
  112. '   events.  The name of an event procedure that's
  113. '   associated with a WithEvents variable always has the
  114. '   variable name as a prefix.
  115. '
  116. Private Sub mwXTimer_Tick()
  117.     ' (Code to test serial port omitted.)
  118.     '
  119.     ' Notify the client.
  120.     RaiseEvent CoffeeReady
  121. End Sub
  122.